﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.ViewModel;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class RoleController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        BLL.Role Bllrole = new BLL.Role();
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Role/Add")]
        public IHttpActionResult AddRole(VM_Role model)
        {
            log.Info($"RoleID:{model.RoleID};RoleName:{model.RoleName}");

            ResultMsg resultMsg = new ResultMsg();
            //验证角色ID RID是否存在  
            if (Bllrole.Exists(model.RoleID))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = "已经存在该角色ID";
                resultMsg.Data = "";
            }
            else
            {
                Model.Role Molrole = new Model.Role();
                Molrole.RoleID = model.RoleID;
                Molrole.RoleName = model.RoleName;
                long flag=Bllrole.Add(Molrole);
                if (flag > 0)
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = flag;
                }
            }
            
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/AuthTest")]
        public IHttpActionResult AuthTest()
        {
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
            resultMsg.Data = "";
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/Role/Sel")]
        public IHttpActionResult GetRoleList()
        {
            log.Info($"获取角色列表");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt= Bllrole.GetAllList().Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到角色信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/Role/Del")]
        public IHttpActionResult DelRole(VM_Role model)
        {
            log.Info($"删除角色");
            ResultMsg resultMsg = new ResultMsg();
            
            if (Bllrole.Delete(model.RoleID))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "删除信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/Role/Edit")]
        public IHttpActionResult UpdateRole(VM_Role model)
        {
            log.Info($"更新角色");
            ResultMsg resultMsg = new ResultMsg();
            Model.Role Molrole = new Model.Role();
            Molrole.AutoID = model.AutoID;
            Molrole.RoleID = model.RoleID;
            Molrole.RoleName = model.RoleName;
            if (Bllrole.Update(Molrole))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "更新信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
    }
}
